#pragma once

namespace bit
{
	template <class T,class Container=deque<T>>
	class Queue
	{
	public:
		void push(const T& x)
		{
			_con.push_back(x);
		}

		void pop(void)
		{
			_con.pop_front();
		}

		T& front(void)
		{
			return _con.front();
		}

		size_t size(void)
		{
			return _con.size();
		}

		bool empty(void)
		{
			return _con.empty();
		}
	private:
		Container _con;
	};

	void test_queue(void)
	{
		//Queue<int> q1;
		Queue<int,list<int>> q1;
		q1.push(1);
		q1.push(2);
		q1.push(3);
		q1.push(4);

		while (!q1.empty())
		{
			cout << q1.front() << " ";
			q1.pop();
		}
		cout << endl;
	}


	template <class T>
	struct less
	{
		bool operator()(const T& x, const T& y) const
		{
			return x < y;
		}
	};

	template <class T>
	struct greater
	{
		bool operator()(const T& x, const T& y) const
		{
			return x > y;
		}
	};

	template <class T, class Container = vector<T>,class Compare=less<T>>
	class priority_queue
	{
		Compare _comFunc;
	public:
		priority_queue(const Compare& comFunc=Compare())
			:_comFunc(comFunc)
		{ }

		template <class InputIterator>
		priority_queue(InputIterator first, InputIterator last,
			const Compare& comFunc = Compare())
			: _comFunc(comFunc)
		{
			while (first != last)
			{
				_con.push_back(*first);
				++first;
			}

			for (int i = (_con.size() - 1 - 1) / 2; i >= 0; i--)
			{
				adjust_down(i);
			}
		}

		void adjust_up(int m)
		{
			int child = m;
			int parent = (child - 1) / 2;
			while (child > 0)
			{
				if(_comFunc(_con[parent], _con[child]))
				{
					swap(_con[child], _con[parent]);
					child = parent;
					parent = (child - 1) / 2;
				}
				else
				{
					break;
				}
			}
		}

		void adjust_down(int m)
		{
			int parent = m;
			int child = 2 * parent + 1;
			while (child<_con.size())
			{
				if ((child + 1 < _con.size()) && (_comFunc(_con[child], _con[child + 1])))
				{
					child++;
				}
	
				if (_comFunc(_con[parent], _con[child]))
				{
					swap(_con[parent], _con[child]);
					parent = child;
					child = 2 * parent + 1;
				}
				else
				{
					break;
				}
			}
		}

		void push(const T& x)
		{
			_con.push_back(x);

			adjust_up(_con.size() - 1);
		}

		void pop(void)
		{
			assert(!_con.empty());

			swap(_con[0], _con[_con.size() - 1]);
			_con.pop_back();
			adjust_down(0);
		}

		const T& top()
		{
			return _con.front();
		}

		bool empty()
		{
			return _con.empty();
		}

		size_t size(void)
		{
			return _con.size();
		}

	private:
		Container _con;
	};

	void test_less(void)
	{
		less<int> l1;
		cout << l1(2,9) << endl;
	}

	void test_priority_queue(void)
	{
		priority_queue<int> pq1;
		pq1.push(19);
		pq1.push(10);
		pq1.push(29);
		pq1.push(1);
		pq1.push(6);
		pq1.push(20);
		pq1.push(60);
		pq1.push(8);

		while (!pq1.empty())
		{
			cout << pq1.top() << " ";
			pq1.pop();
		}
		cout << endl;
	}

	void test_priority_queue1(void)
	{
		int a[] = { 1, 4, 2, 7, 8, 9 };
		priority_queue<int> pq2(a, a + 6);
		while (!pq2.empty())
		{
			cout << pq2.top() << " ";
			pq2.pop();
		}
		cout << endl;
	}
}